package cn.yitun.chat.yitunserverrunsingleboot.chat.properties;

import cn.yitun.chat.yitunserverrunsingleboot.chat.factory.YamlPropertyLoaderFactory;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 数据库配置类
 * @author chenyitun
 * @date 2020-06-07
 */
@Data
@NoArgsConstructor
@Configuration
@PropertySource(value = {"classpath:config/db.yml"}, factory = YamlPropertyLoaderFactory.class, ignoreResourceNotFound = true)
public class DbProperties {
    //master
    @Value("${db.master.url}")
    private String masterUrl;
    @Value("${db.master.username}")
    private String masterUsername;
    @Value("${db.master.password}")
    private String masterPassword;
    //slave
    @Value("${db.slave.url}")
    private String slaveUrl;
    @Value("${db.slave.username}")
    private String slaveUsername;
    @Value("${db.slave.password}")
    private String slavePassword;
    //最大连接池数量
    @Value("${db.driverClassName}")
    private String driverClassName;
    //初始化连接池数量
    @Value("${db.filters}")
    private String filters;
    //最小连接池数量
    @Value("${db.maxActive}")
    private Integer maxActive;
    //获取连接时最大等待时间,单位毫秒
    @Value("${db.initialSize}")
    private Integer initialSize;
    //
    @Value("${db.minIdle}")
    private Integer minIdle;
    //
    @Value("${db.maxWait}")
    private Integer maxWait;
    //
    @Value("${db.timeBetweenEvictionRunsMillis}")
    private Long timeBetweenEvictionRunsMillis;
    //
    @Value("${db.minEvictableIdleTimeMillis}")
    private Long minEvictableIdleTimeMillis;
    //
    @Value("${db.validationQuery}")
    private String validationQuery;
    //申请连接的时候检测，如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效
    @Value("${db.testWhileIdle}")
    private Boolean testWhileIdle;
    //申请连接时执行validationQuery检测连接是否有效
    @Value("${db.testOnBorrow}")
    private Boolean testOnBorrow;
    //归还连接时执行validationQuery检测连接是否有效
    @Value("${db.testOnReturn}")
    private Boolean testOnReturn;
    //
    @Value("${db.maxOpenPreparedStatements}")
    private Integer maxOpenPreparedStatements;
    //对于长时间不使用的连接强制关闭
    @Value("${db.removeAbandoned}")
    private Boolean removeAbandoned;
    //超过30分钟开始关闭空闲连接
    @Value("${db.removeAbandonedTimeout}")
    private Integer removeAbandonedTimeout;
    //将当前关闭动作记录到日志
    @Value("${db.logAbandoned}")
    private Boolean logAbandoned;


    @Override
    public String toString() {
        return "DbProperties{" +
                "masterUrl='" + masterUrl + '\'' +
                ", masterUsername='" + masterUsername + '\'' +
                ", masterPassword='" + masterPassword + '\'' +
                ", slaveUrl='" + slaveUrl + '\'' +
                ", slaveUsername='" + slaveUsername + '\'' +
                ", slavePassword='" + slavePassword + '\'' +
                ", driverClassName='" + driverClassName + '\'' +
                ", filters=" + filters +
                ", maxActive=" + maxActive +
                ", initialSize=" + initialSize +
                ", minIdle=" + minIdle +
                ", maxWait=" + maxWait +
                ", timeBetweenEvictionRunsMillis=" + timeBetweenEvictionRunsMillis +
                ", minEvictableIdleTimeMillis=" + minEvictableIdleTimeMillis +
                ", validationQuery='" + validationQuery + '\'' +
                ", testWhileIdle=" + testWhileIdle +
                ", testOnBorrow=" + testOnBorrow +
                ", testOnReturn=" + testOnReturn +
                ", maxOpenPreparedStatements=" + maxOpenPreparedStatements +
                ", removeAbandoned=" + removeAbandoned +
                ", removeAbandonedTimeout=" + removeAbandonedTimeout +
                ", logAbandoned=" + logAbandoned +
                '}';
    }
}